use "${datadir}\HTB_main", clear	// 5 sec

ren yearbirth dob
merge m:1 pcd dob using "${datadir_psd}\work_psd_stock_2017H2", keep(1 3) ///
	gen(merge007) keepusing(frnoflendcurr isthecurrrat irate_type)

merge m:1 schemeid using ///	
	"${datadir}\HCA_redemptions_all", ///
	keep(1 3) gen(merge_redemption)



*--- Regressions 
// On incentivised rate
gen incent = isthecurrratincenrate == "Current Rate Incentivised - Yes"
tab irate_type incent

sum incent if merge007 == 3 & subsample, meanonly
assert round(r(mean), 0.01) == 0.80


// internal vs external remortgage
gen incenti = incent & lender_id == frnoflend
gen incente = incent & lender_id != frnoflend

sum incenti if merge007 == 3 & subsample, meanonly
assert round(r(mean), 0.01) == 0.61

sum incente if merge007 == 3 & subsample, meanonly
assert round(r(mean), 0.01) == 0.18



// Regression
gen constant = 1
encode laua, gen(la)
gen gross_income_log = log(gross_income)


reghdfe incent htb_dummy i.lender_id ///
	gross_income_log age_borrower i.advtype ///
	i.employment i.income_basis purchaseprice ///
	if merge007 == 3 & subsample, ///
	absorb(la datem) vce(cluster la datem) // to get e(sample)
gen mysample = e(sample)


eststo clear

// All refinancing
eststo: reghdfe incent htb_dummy if merge007 == 3 & subsample & mysample, ///
	absorb(constant) vce(cluster la datem)
	estadd local controls 	""
	estadd local FE 	""
	estadd local cluster "LA, sale month"
	
eststo: reghdfe incent htb_dummy i.lender_id ///
	gross_income_log age_borrower i.advtype ///
	i.employment i.income_basis purchaseprice ///
	if merge007 == 3 & subsample & mysample, ///
	absorb(la datem) vce(cluster la datem)
	estadd local controls 	"Yes"
	estadd local FE 	"LA, sale month"
	estadd local cluster "LA, sale month"

	
// Same-lender refinancing
eststo: reghdfe incenti htb_dummy if merge007 == 3 & subsample & mysample, ///
	absorb(constant) vce(cluster la datem)
	estadd local controls 	""
	estadd local FE 	""
	estadd local cluster "LA, sale month"

eststo: reghdfe incenti htb_dummy i.lender_id ///
	gross_income_log age_borrower i.advtype ///
	i.employment i.income_basis purchaseprice ///
	if merge007 == 3 & subsample & mysample, ///
	absorb(la datem) vce(cluster la datem)
	estadd local controls 	"Yes"
	estadd local FE 	"LA, sale month"
	estadd local cluster "LA, sale month"
	
	
// Different-lender refinancing
eststo: reghdfe incente htb_dummy if merge007 == 3 & subsample & mysample, ///
	absorb(constant) vce(cluster la datem)
	estadd local controls 	""
	estadd local FE 	""
	estadd local cluster "LA, sale month"

eststo: reghdfe incente htb_dummy i.lender_id ///
	gross_income_log age_borrower i.advtype ///
	i.employment i.income_basis purchaseprice ///
	if merge007 == 3 & subsample & mysample, ///
	absorb(la datem) vce(cluster la datem)
	estadd local controls 	"Yes"
	estadd local FE 	"LA, sale month"
	estadd local cluster "LA, sale month"
	
esttab using "${tabledir}\rmg_r6dec.tex", b(3) se star(* 0.10 ** 0.05 *** 0.01) ///
	coeflabel(htb_dummy "EL") replace keep(htb_dummy) ///
	stats(controls FE cluster r2 N, fmt ( 0 0 0 %4.2fc %10.0fc) ///
	labels( `"\textsc{Regression controls}"' `"\textsc{Fixed effects}"' `"\textsc{SE clustering}"') )  ///
	mtitle("All" "All" "Same lender" "Same lender" "Different lender" "Different lender")  
